////////////////////////////////////////////////////////////////////////////////
//
//	Original file Name: InputDevice.java
//	Originating Author: JWhitefield
//	
//	Description:
//
////////////////////////////////////////////////////////////////////////////////

package com.jjw.game.input;

import java.util.ArrayList;
import java.util.List;

import com.jjw.game.ITickable;

public class InputDevice implements IInputPlayer,IInputMenu, ITickable {


	//--------------------------------------------------------------------------
	//
	//Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	// Public
	//----------------------------------
	
	//----------------------------------
	//  Getters / Setters
	//----------------------------------
	
	//----------------------------------
	// Private
	//----------------------------------
	
	/**
	 * List of the input buttons which this input device is using
	 */
	private List<InputButton> registeredInputButtons = new ArrayList<InputButton>();
	
	/**
	 * Directional input buttons
	 */
	private InputButton upInputButton;
	private InputButton downInputButton;
	private InputButton leftInputButton;
	private InputButton rightInputButton;
	
	/**
	 * Player input buttons
	 */
	private InputButton attackInputButton;
	
	/**
	 * Menu input buttons
	 */
	private InputButton selectInputButton;
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	// Public
	//----------------------------------
	
	InputDevice() {
		//Creates the list of input buttons
		this.createInputButtons();
	}
	
	/**
	 * Adds this button to the registered input buttons
	 * 
	 * @param inputButton	the input button to register
	 */
	protected void registerInputButton( InputButton inputButton ) {
		
		//Add the input button to the registered list
		this.registeredInputButtons.add(inputButton);
	}
		
	/**
	 * Resets all the input button states to up
	 */
	void resetInputButtons(){
		
		//Loop through all the registered keys and set them all to up 
		for (int i = 0; i < this.registeredInputButtons.size(); i++) 
			this.registeredInputButtons.get(i).toggle(false);
	}
	
	//----------------------------------
	// Private
	//----------------------------------
	
	/**
	 * Creates each of the input buttons required 
	 */
	private void createInputButtons() {
		
		//Create the directional buttons
		this.upInputButton = new InputButton();
		this.downInputButton = new InputButton();
		this.leftInputButton = new InputButton();
		this.rightInputButton = new InputButton();
		
		//Create the player buttons
		this.attackInputButton = new InputButton();
		
		//Create the menu buttons
		this.selectInputButton = new InputButton();
		
		//Register all the keys
		this.registerInputButton( this.upInputButton );
		this.registerInputButton( this.downInputButton );
		this.registerInputButton( this.rightInputButton );
		this.registerInputButton( this.leftInputButton );
		
		this.registerInputButton( this.attackInputButton );
		
		this.registerInputButton( this.selectInputButton );
	}
	
	//----------------------------------
	// Overriden / Implmented
	//----------------------------------
	
	@Override
	public InputButton getUpInputButton() {
		return this.upInputButton;
	}

	@Override
	public InputButton getDownInputButton() {
		return this.downInputButton;
	}

	@Override
	public InputButton getLeftInputButton() {
		return this.leftInputButton;
	}

	@Override
	public InputButton getRightInputButton() {
		return this.rightInputButton;
	}

	@Override
	public InputButton getSelectInputButton() {
		return this.selectInputButton;
	}

	@Override
	public InputButton getAttackInputButton() {
		return this.attackInputButton;
	}
	
	@Override
	public void tick() {
		
		//Loop through the registered inputs and tick them
		for( int i = 0; i < this.registeredInputButtons.size(); i++ )
			this.registeredInputButtons.get(i).tick();
	}
}
